package com.lc.pianostudent.model;



import com.lc.pianostudent.BaseApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 */

public final class UserModule {

    private List<UserStateCallBack> list = new ArrayList<>();

    public void login(String uid) {

        change(uid);

    }

    public void exit() {

        change("");

    }

    private void change(String uid) {

        BaseApplication.BasePreferences.saveUID(uid);

        if (uid.equals("")) {

            for (UserStateCallBack userStateCallBack: list) {

                userStateCallBack.onExit();

            }

        } else {

            for (UserStateCallBack userStateCallBack: list) {

                userStateCallBack.onLoing(uid);

            }

        }

    }

    public void addUserStateCallBack(UserStateCallBack userStateCallBack) {

        String uid = BaseApplication.BasePreferences.readUID();

        if (uid.equals("")) {

            userStateCallBack.onExit();

        } else {

            userStateCallBack.onLoing(uid);

        }

        list.add(userStateCallBack);

    }

    public void remove(UserStateCallBack userStateCallBack) {

        list.remove(userStateCallBack);

    }

    public static class UserStateCallBack {

        protected void onLoing(String uid){}

        protected void onExit(){}

    }

    public static UserModule getInstance() {

        return UserModuleHolder.INSTANCE;

    }

    private static class UserModuleHolder {

        private static final UserModule INSTANCE = new UserModule();

    }

}
